package com.cdacp.openapi.example;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cdacp.openapi.Client;
import com.cdacp.openapi.common.Signature;
import com.cdacp.openapi.test.TestBase;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 批量创建接口
 */
public class CreateBatchTest extends TestBase {

    String url = "http://localhost:8081";
    String appId = "20201224791753573959467008";
    String privateKey="MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCgTOwp23IFz2lixjGuxIm5qtT53GkH6WuU1FK7+kdeK8ejFJDr3Vi/DdbNeIA7KLP5UDjGG8/oD+xfUaGCPCsOLzeMeO4ubPMNF4OZ2exEC+6uQwCA5Cg2eoUWkagpQfZRESiPFnU3dLmJhQIYxM4UhX+gwHzLoxvEyBN9fzG9Ychg9xlMkBCoPrqFK85ycDshQofxgBPb2KDOG9xw2Xz/6+nHkV9SI1u/7uD6jChCBXoAHNr+O8TvWIOow3fIe5DpqptVRYWAaMUTNdk3Bxffxz6rEUQ+qxw8cPmRW9VTjgzHuyBEDS4HwJa80jXEV12DGXE/PCRQriFgPvdGQHHvAgMBAAECggEBAJoGz7W4gmtKzLx4LCz99EhO+a3MN6++ffzXt1egOQsnjgAJNucsHKYXqhpseupdRfF9P9A70R3ujvVX/pShM83BWOLFIDfDwBcITQ978iXAQWt4PfebQ3h1aYQyGR7rnr5uekZjEeZjZhPZlhYupMXbMLNJwZ/57ii7Bt9DZIvO7HRmu4QekFs9bTMtuNPUw36ME9FizslxxzmBsDG2a4/29lPPAUHA363nYILJ7fd+IVpYVUaerl/oXicFpC10S1OkQ29NbGg1R4VNxIc5TqC74fSshQu/sFhztFgl5ci2ZLPBOJuMaGKuT6mi9ZRnOQsNzNrVWs1V7nzlwoFuxUECgYEA53HyVUYbdTm90L+EnU7wk79bLNQFx1fjfgHPLsiOHwZUdWb8knzMro/Dw+sdjVrvHHb/FhMRJw9XlEE1wrXj21Igm5WPKRItbar8I0ri77OxS7B7hhfsJ0u1xICFinUzheUhDYlbQ2h872QXvZ1zBZfoifKTmHneC0gaBnLNZYkCgYEAsU6vwKXGNDLAsBMsskMzljJ4uK0V/8chWO85kIUP4OJYF3c+cufmc8IyofRIPepW1mteIe/ZkR2fV+bEBeDQr3Y0jNlaTr+QPgNNn4bXwRm3de2Q36oiJlM1eNOnQdlo1LwrOBgpA4WPhNcuWY0KofaDdkPqyac6xteNx3HLtbcCgYEAytjMer6CTEC6Q13HvhO0uNmK8IQ3OFqoUQUG78ro/PoQRgFoNn9Y6VmpS2oisYRUc088DL3TV7eTm6V3l9rRBKU7XQE8wtNiq26jo877jPih2GOqL4GCAYLSoZG++PX1nVGPoYyUR0Up0qqYQCDh8bINJfJMN+P/n0WnQaWWrXkCgYB/7s2jTDmp+iDMKtmqOhDjw8EVR1ZDOrQWDm5Tl57wXYDAVbrKgAbo6WtNLgxeGFLV3ZyLEhvCuEP70bcIosEQiVK7eizN+yyhghKlE09jtiGIxoQszf5b9tfzOYxKWAtqy+bHT69wKhN2/ygjV0AfwtXyJGkpXS9sv6U+Dh61GwKBgQCuuxrfFGvqRniVtMAu3n6XjGpd7FnjAfceojUmekoEntvuLbHyJ/dT41EbW7maH5pc1QRhOPbmj6ddGw9EVWx4H8AkgMfoLD2CpRM+s8YjdT3wikXJ5LT6r6N7R47Uf/3anEL3K/WTo8aEEsENyhD9mQdRzBbVa6VKj+QaWpudLg==";
    /**
     参数	            类型	    是否必填	    最大长度	    描述	            示例值
     app_id	        String	是	        32	    cdacp分配给开发者的应用ID	2014072300007148
     method	        String	是	        128	    接口名称	trade.fastpay.refund.query
     format	        String	否	        40	    仅支持JSON	JSON
     charset	        String	是	        10	    请求使用的编码格式，如utf-8,gbk,gb2312等	utf-8
     sign_type	    String	是	        10	    商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2	RSA2
     sign	        String	是	        344	    商户请求参数的签名串，详见签名	详见示例
     timestamp	    String	是	        19	    发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"	2014-07-24 03:07:50
     version	        String	是	        3	    调用的接口版本，固定为：1.0	1.0
     app_auth_token	String	否	        40	    详见应用授权概述
     biz_content	    String	是		请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档
     */
    // 这个请求会路由到服务
    @Test
    public void testFile() {
        Client client = new Client(url, appId, privateKey);
        String root = System.getProperty("user.dir");
        Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
                .method("create.batch.param.post")
                .version("1.0")
                .bizContent(new BizContent().add("appId", appId).add("packageName","com.demo"))
                // 添加文件
                .addFile("file", new File(root + "/src/main/resources/test.xlsx"))
                .callback((requestInfo, responseData) -> {
                    System.out.println(responseData);
                    JSONObject jsonObject = JSON.parseObject(responseData);
                    JSONObject data = jsonObject.getJSONObject(requestInfo.getDataNode());
                    System.out.println(data);
                })
                ;

        client.execute(requestBuilder);
    }
    static class BizContent extends HashMap<String, Object> {
        public BizContent add(String key, Object value) {
            this.put(key, value);
            return this;
        }
    }


}
